package cn.qin.xhangapi.util.qiniuyun;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;

/**
 * 七牛工具类
 *
 * @author 多仔ヾ
 */
@Component
@EnableConfigurationProperties(QiniuProperties.class)
public class QiniuUtil {

    @Resource
    QiniuProperties qiniuProperties;
    @Resource
    UploadManager uploadManager;
    @Resource
    BucketManager bucketManager;
    @Resource
    Auth auth;
    
    StringMap putPolicy;

    /**
     * 上传文件
     *
     * @author 多仔ヾ
     * @param file 文件对象
     * @return java.lang.String
     */
    public String uploadFile(File file) throws QiniuException {
        Response response = this.uploadManager.put(file, null, getUploadToken());
        int retry = 0;
        while (response.needRetry() && retry < 3) {
            response = this.uploadManager.put(file, null, getUploadToken());
            retry++;
        }

        JSONObject putRet = JSONUtil.parseObj(response.bodyString());
        String path = qiniuProperties.getPrefix() + "/" + putRet.getStr("key");
        return path;
    }

    /**
     * 上传文件
     *
     * @author 多仔ヾ
     * @param inputStream
     * @return java.lang.String
     */
    public String uploadFile(InputStream inputStream) throws QiniuException {
        Response response = this.uploadManager.put(inputStream, null, getUploadToken(), null, null);
        int retry = 0;
        while (response.needRetry() && retry < 3) {
            response = this.uploadManager.put(inputStream, null, getUploadToken(), null, null);
            retry++;
        }

        JSONObject putRet = JSONUtil.parseObj(response.bodyString());
        String path = qiniuProperties.getPrefix() + "/" + putRet.getStr("key");
        return path;
    }

    /**
     * 删除文件
     *
     * @author 多仔ヾ
     * @param key Key
     * @return com.qiniu.http.Response
     */
    public Response delete(String key) throws QiniuException {
        Response response = bucketManager.delete(qiniuProperties.getBucket(), key);
        int retry = 0;
        while (response.needRetry() && retry++ < 3) {
            response = bucketManager.delete(qiniuProperties.getBucket(), key);
        }

        return response;
    }

    public void afterPropertiesSet() {
        this.putPolicy = new StringMap();
        putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
    }

    private String getUploadToken() {
        return this.auth.uploadToken(qiniuProperties.getBucket(), null, 3600, putPolicy);
    }

}